package business.batchPosCodeCorp

import java.util.Collection;

import org.apache.log4j.Logger;

import business.coding.CenterWeightAndWords
import business.database.*;
import business.ViewLogger;
import dao.Center

class PreCodeCorp {

	private Logger _log = Logger.getLogger(PreCodeCorp);
	
	/**
	 * Obtiene todos los centros candidatos para el idcodecorp indicado
	 * @param idcodecorp
	 * @return
	 */
	public List<CenterWeightAndWords> getPreCodeCorpsCenters(int idcodecorp) {
		
		List<CenterWeightAndWords> censW = new ArrayList<CenterWeightAndWords>();
		
		Database.getInstance().sqlnewcorps.eachRow ("select * from PreCodeCorp where idcodecorp = ${idcodecorp}" , {
			int id = it[0]
			String corp = it[1]
			String iso = it[2]
			String prov = it[3]
			String instit = it[4]
			String org = it[5]
			String dpto = it[6]
			Center cen = new Center(prov, instit, org, dpto);
			cen.setIdCe idcodecorp
			cen.setISO(iso)
			int range = it[7]
			double permatch = it[8]
			CenterWeightAndWords cenW = new CenterWeightAndWords();
			cenW.setCenter cen
			cenW.set_context_by_range range
			cenW.set_permatch permatch
			
			fillMaesorgHelp(cenW);
			
			censW.add(cenW)
		})
		
		return censW;
	}
	
	/**
	* Obtiene la ayuda del maesorg y la inserta en el campo notas del centro
	* @param cenW
	*/
   private void fillMaesorgHelp(CenterWeightAndWords cenW) {
	   
	   try {
		   String help = "";
		   MaesorgUtils.getMaesorgHelp (cenW.getCenter()).each {
			   help += " / ${it}"
		   }
	   
		   cenW.set_notes help;
	   } catch (Exception e) {
	   _log.error "Error buscando la ayuda del maesorg"
	   }
   }

   /**
    * Obtiene el corp de idcodecorp
    * @param idcodecorp
    * @return
    */
	public String selectCorp(int idcodecorp) {
		def corp = Database.getInstance().sqlnewcorps.firstRow ("select corp from precodecorp where idcodecorp = ${idcodecorp}")
		if (corp != null && corp[0] != null)
			return corp
		else return ""
	}
}
